# -*- coding: utf-8 -*-
# @author : wanglei
# @date : 2020/4/15 11:05 PM
# @description : 自定义的小部件

from manimlib.imports import *


"""
解释文字【类似字幕的作用】,使用动态小喇叭 TODO: 由于小喇叭的动态效果不如意，故暂不使用，后期再优化
"""
def say_with_dynamic_voice(current_scene, position, words):
    if not isinstance(current_scene, Scene):
        raise Exception("current_scene must be Scene")
    # 创建动态小喇叭
    voices = []
    for i in range(3):
        voice = SVGMobject('voice'+str(i)+'.svg').set_fill(WHITE).set_stroke(color=WHITE, width=0.1)
        voice.scale(0.15)
        voice.move_to(position,aligned_edge=LEFT)
        voices.append(voice)

    # 播放
    # for i in range(3):
    #     current_scene.play(FadeIn(voices[i]), run_time=0.1)

    time_tracker = ValueTracker()

    def update_voice(obj):
        # print('time_tracker.get_value()', time_tracker.get_value())
        index = int(time_tracker.get_value())%3 # 不是整数，怀疑是时间小数
        # print('index',index)
        new_voice = SVGMobject('voice' + str(index) + '.svg').set_fill(WHITE).set_stroke(color=WHITE, width=0.1)
        new_voice.scale(0.15)
        new_voice.move_to(position, aligned_edge=LEFT)
        obj.become(new_voice)

    voice = SVGMobject('voice0.svg').set_fill(WHITE).set_stroke(color=WHITE, width=0.1)
    voice.scale(0.15)
    voice.move_to(position, aligned_edge=LEFT)
    voice.add_updater(update_voice)
    current_scene.add(voice)

    # 添加文字和文本框
    text = TextMobject(words)
    text.scale(0.6)
    text.next_to(voices[2])
    current_scene.play(time_tracker.increment_value, 10, Write(text), rate_func=linear,run_time=2.0) # 10是increment函数自增到的最大值
    say_objs = list(reversed(current_scene.get_mobjects()))[:4]
    say_gp = Group(*say_objs) # *多个参数传入
    current_scene.play(FadeOut(say_gp))


"""
解释文字【类似字幕的作用】，静态小喇叭
"""
def say(current_scene, position, words):
    if not isinstance(current_scene, Scene):
        raise Exception("current_scene must be Scene")

    voice = SVGMobject('voice2.svg').set_fill(WHITE).set_stroke(color=WHITE, width=0.1)
    voice.scale(0.15)

    # 添加文字和文本框
    text = TextMobject(words)
    text.scale(0.6)
    text.next_to(voice, RIGHT)
    say_gp = Group(voice, text)
    say_gp.move_to(position)
    current_scene.play(ShowCreationThenFadeOut(say_gp), run_time=len(words)/5)


class Logo_gp(VGroup):
    def __init__(self, **kwargs):
        VGroup.__init__(self, **kwargs)
        # 坐标、文本、颜色
        coordinate_list = [[np.array([-0.4, -0.4, 0]), np.array([0.4, -0.4, 0]), np.array([0.4, 0.4,0]), np.array([-0.4,  0.4, 0])],
                           [np.array([-0.4,  0.4, 0]), np.array([0.21279006, 0.91419192, 0]), np.array([-0.30140185,  1.52698198, 0]), np.array([-0.91419192,  1.01279006, 0])],
                           [np.array([0.21279006, 0.91419192, 0]), np.array([0.4, 0.4, 0]), np.array([0.91419192, 0.58720994, 0]), np.array([0.72698198, 1.10140185, 0])]]
        intro = '树先生'
        colors = ['#ff2d51', '#009999', '#00CC00']
        # 创建logo
        for i, coordinates in enumerate(coordinate_list):
            polygon = Polygon(*coordinates)
            intro_text = Text(intro[i], font='smmyingbi')
            intro_text.scale(0.7)
            intro_text.move_to(polygon.get_center())
            polygon.set_stroke(width=0)
            polygon.set_fill(colors[i], opacity=1)
            self.add(polygon, intro_text)
